﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the Xcoordination Application Space
 * ("XcoAppSpace") http://www.xcoordination.com
 *
 * Copyright (C) 2009 Xcoordination http://www.xcoordination.com
 *
 * XcoAppSpace is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General
 * Public License as published by the Free Software Foundation.
 *
 * XcoAppSpace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 * or write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Collections.Generic;
using XcoAppSpaces.Core.Ports;

namespace XcoAppSpaces.Core
{
	/// <summary>
	/// XcoAppSpace extension methods for simplifying usage of the application space.
	/// </summary>
    public static class XcoAppSpaceExtensions
	{
		#region Run & Connect for easy Single-Port Hosting
		/// <summary>
		/// Runs a single port with the given type.
		/// </summary>
		/// <typeparam name="T">The type of the port.</typeparam>
		/// <param name="space">The space.</param>
		/// <param name="name">The name of the port.</param>
		/// <param name="handler">The handler that should be executed for incoming items.</param>
		/// <returns>The port that has been created.</returns>
		public static Port<T> Run<T>(this IXcoAppSpace space, string name, Action<T> handler)
		{
			var port = space.RunWorker<Port<T>, Port<T>>(name);
			port.RegisterReceiver(handler);
			return port;
		}

		/// <summary>
		/// Runs a single port with the given type.
		/// </summary>
		/// <typeparam name="T">The type of the port.</typeparam>
		/// <param name="space">The space.</param>
		/// <param name="handler">The handler that should be executed for incoming items.</param>
		/// <returns>The port that has been created.</returns>
		public static Port<T> Run<T>(this IXcoAppSpace space, Action<T> handler)
		{
			return space.Run(null, handler);
		}

		/// <summary>
		/// Connects to a single port with the given type.
		/// </summary>
		/// <typeparam name="T">The type of the port.</typeparam>
		/// <param name="space">The space.</param>
		/// <param name="address">The address of the port.</param>
		/// <returns>Proxy for posting messages to the port.</returns>
		public static Port<T> Connect<T>(this IXcoAppSpace space, string address)
		{
			return space.ConnectWorker<Port<T>>(address);
		}

		#endregion


		///// <summary>
		///// Creates a timed action with the given space, interval and action.
		///// </summary>
		///// <param name="space">The XcoAppSpace.</param>
		///// <param name="interval">Timer interval.</param>
		///// <param name="action">The action that should be invoked.</param>
		///// <returns>The created XcoTimedAction object. Call Invoke() or InvokeDelayed() to start the timed action. </returns>
		//public static XcoTimedAction CreateTimedAction(this IXcoAppSpace space, TimeSpan interval, Func<XcoTimedAction.Continuation> action)
		//{
		//	return new XcoTimedAction(space.DefaultDispatcherQueue, interval, action);
		//}
	}
}
